Defined in: src/constructs/rest-api/rest-api.ts:147
A REST API construct that creates and configures an AWS API Gateway with best practices.
This construct simplifies the creation of REST APIs by providing:
- Regional endpoint configuration for better performance and security
- Automatic CloudWatch logging with configurable retention
- Distributed tracing and metrics collection
- Stage-aware CORS configuration
- Security-focused defaults (disabled execute-api endpoint)
- Structured naming conventions for multi-environment deployments
The construct automatically sets up comprehensive observability through CloudWatch logs, AWS X-Ray tracing, and CloudWatch metrics. For staging environments, it can apply permissive CORS settings suitable for development, while production environments require explicit CORS configuration.
Example
// Basic production API
const api = new RestApi(this, 'UserApi', {
stageName: 'prod',
description: 'User Management API',
deploy: true,
defaultCorsPreflightOptions: {
allowOrigins: ['https://myapp.com'],
allowMethods: ['GET', 'POST', 'PUT', 'DELETE'],
allowHeaders: ['Content-Type', 'Authorization'],
allowCredentials: true,
},
});
// Add resources and methods to the API
const users = api.api.root.addResource('users');
users.addMethod('GET', new apigw.LambdaIntegration(getUsersFunction));
users.addMethod('POST', new apigw.LambdaIntegration(createUserFunction));
// Development API with permissive CORS
const devApi = new RestApi(this, 'DevApi', {
stageName: 'dev',
description: 'Development API',
deploy: true,
isStagingEnvironment: true, // Enables permissive CORS
});
Remarks
The construct creates the following AWS resources:
- API Gateway REST API with regional endpoint
- CloudWatch Log Group for access logging (1-day retention)
- API Gateway deployment with stage configuration
- CloudWatch role for API Gateway logging
Security considerations:
- Execute API endpoint is disabled by default for security
- CloudWatch role is automatically created for audit logging
- CORS should be explicitly configured for production environments
Extends
Construct
Constructors
Constructor
new RestApi(
scope,id,props):RestApi
Defined in: src/constructs/rest-api/rest-api.ts:242
Creates a new RestApi construct with comprehensive API Gateway configuration.
The constructor sets up a REST API with the following features:
- Regional endpoint for improved performance and security
- CloudWatch logging with 1-day retention for cost optimization
- AWS X-Ray tracing for request tracking
- CloudWatch metrics for monitoring
- Stage-aware CORS configuration
- Disabled execute-api endpoint for security
Parameters
scope
Construct
The parent construct (typically a Stack or Stage)
id
string
A unique identifier for this construct within the scope
props
Configuration properties for the REST API
Returns
RestApi
Example
// Production API with explicit CORS
const prodApi = new RestApi(this, 'ProductionAPI', {
stageName: 'prod',
description: 'Production User Management API',
deploy: true,
defaultCorsPreflightOptions: {
allowOrigins: ['https://myapp.com', 'https://admin.myapp.com'],
allowMethods: ['GET', 'POST', 'PUT', 'DELETE'],
allowHeaders: ['Content-Type', 'Authorization', 'X-Api-Key'],
allowCredentials: true,
},
});
// Development API with auto-CORS
const devApi = new RestApi(this, 'DevelopmentAPI', {
stageName: 'dev',
description: 'Development API',
deploy: true,
isStagingEnvironment: true,
});
Throws
Will throw an error if the API Gateway creation fails
Remarks
The construct automatically creates:
- A CloudWatch Log Group named
${id}ApiLogswith 1-day retention - An API Gateway REST API named
${id}-api-${stageName} - A deployment stage named 'api' with logging and tracing enabled
- A CloudWatch role for API Gateway service logging
Overrides
Construct.constructor
Properties
api
readonlyapi:RestApi
Defined in: src/constructs/rest-api/rest-api.ts:173
The underlying AWS API Gateway REST API instance.
This provides direct access to the API Gateway for adding resources, methods, and integrations. You can use this to:
- Add resources:
api.root.addResource('users') - Add methods:
resource.addMethod('GET', integration) - Configure authorizers, validators, and other API Gateway features
Example
const restApi = new RestApi(this, 'MyApi', { ... });
// Add a resource
const users = restApi.api.root.addResource('users');
// Add methods with integrations
users.addMethod('GET', new apigw.LambdaIntegration(listUsersFunction));
users.addMethod('POST', new apigw.LambdaIntegration(createUserFunction));
// Add nested resources
const userById = users.addResource('{id}');
userById.addMethod('GET', new apigw.LambdaIntegration(getUserFunction));
node
readonlynode:Node
Defined in: node_modules/.pnpm/constructs@10.0.5/node_modules/constructs/lib/construct.d.ts:305
The tree node.
Stability
stable
Inherited from
Construct.node
Methods
toString()
toString():
string
Defined in: node_modules/.pnpm/constructs@10.0.5/node_modules/constructs/lib/construct.d.ts:319
Returns a string representation of this construct.
Returns
string
Stability
stable
Inherited from
Construct.toString
isConstruct()
staticisConstruct(x):x is Construct
Defined in: node_modules/.pnpm/constructs@10.0.5/node_modules/constructs/lib/construct.d.ts:299
(deprecated) Checks if x is a construct.
Parameters
x
any
Any object.
Returns
x is Construct
true if x is an object created from a class which extends Construct.
Deprecated
use x instanceof Construct instead
Inherited from
Construct.isConstruct